package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private OrderMapper ordersMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDate begin, LocalDate end) {

        Integer newUsers = userMapper.getNewUser(begin, end);

        Integer allOrdersCount = reportMapper.getOrderCountByStatus(begin,end,null);
        if (allOrdersCount == null){
            allOrdersCount = 0;
        }
        Integer validOrderCount = reportMapper.getOrderCountByStatus(begin,end, Orders.COMPLETED);
        if (validOrderCount == null){
            validOrderCount = 0;
        }

        double orderCompletionRate = 0.;
        if (allOrdersCount != 0){
            orderCompletionRate = (double) validOrderCount / allOrdersCount;
        }

        Double turnover = reportMapper.getTurnoverStatistics(begin.toString(),end.toString());
        if (turnover == null){
            turnover = 0.;
        }
        double unitPrice = 0.;
        if (validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
        }
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setMealMapper.getSetmealOverview(1);
        Integer discontinued = setMealMapper.getSetmealOverview(0);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer sold = dishMapper.getDishesOverview(1);
        Integer discontinued = dishMapper.getDishesOverview(0);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {


        Integer cancel = ordersMapper.getOrderOverview(LocalDate.now(), LocalDate.now().plusDays(1),Orders.CANCELLED);
        Integer complete = ordersMapper.getOrderOverview(LocalDate.now(), LocalDate.now().plusDays(1),Orders.COMPLETED);
        Integer CONFIRMED = ordersMapper.getOrderOverview(LocalDate.now(), LocalDate.now().plusDays(1),Orders.CONFIRMED);
        Integer TO_BE_CONFIRMED = ordersMapper.getOrderOverview(LocalDate.now(), LocalDate.now().plusDays(1),Orders.TO_BE_CONFIRMED);
        Integer all = ordersMapper.getOrderOverview(LocalDate.now(), LocalDate.now().plusDays(1),null);
        return OrderOverViewVO.builder()
                .cancelledOrders(cancel)
                .completedOrders(complete)
                .allOrders(all)
                .deliveredOrders(CONFIRMED)
                .waitingOrders(TO_BE_CONFIRMED)
                .build();

    }
}
